GBML manual / Paths

Paths

A path is a route drawn once in the editor, which any number of instances can be told to follow, with a choice of what happens when they reach the end.

What a path is

A path is a list of points, in order, joined by straight lines. You draw it once in the editor's Paths tab, give it a name, and from then on it is an asset like a sprite or a room: any code in the project can refer to it by that name. The Paths tab covers drawing one.

Two settings change its shape:

An instance is one copy of an object in a room. To move one along a route, you hand it a path instead of giving it a speed. Motion and pathing explains when to choose which.

Following a path

path_start sets an instance following. It takes four things:

// Create event of a guard
path_start(path_patrol, 1.5, PATH_LOOP, true);
  1. The path - here path_patrol, a path drawn in the editor.
  2. The speed, in pixels per frame along the route. This is a real distance, not a fraction of the route. A frame is one picture of the game, and there are 60 a second.
  3. The end action - what happens at the end. See below.
  4. Relative. With false, the instance follows the path exactly where it was drawn. With true, the path is treated as a set of steps from wherever the instance stands when you call it. That is what lets ten enemies placed in ten different spots all patrol the same drawn shape, each around itself.

The instance jumps onto the first point on the same frame. It is not eased in. The engine then moves it along the route each frame, after ordinary movement, and it sets x and y directly. While an instance follows a path, its hsp/vsp/speed have no visible effect.

An instance follows one path at a time. Calling path_start again switches it to the new one. path_stop ends the following and leaves the instance where it stands.

A path does not check for walls. An instance on a path passes through solid tiles. To patrol a route that must respect walls, leave path_start alone and read points off the path instead. path_x_at(p, pos) and path_y_at(p, pos) give the position at pos along the route, without moving anything. pos runs from 0 (the start) to 1 (the end), and a value outside that range is held to it. Walk toward the point with move_slide.

Where the instance is on its route

Four variables on the instance itself describe the following:

VariableMeaning
pathwhich path it is following, or nothing if none
path_poshow far along it is, from 0 to 1
path_speedhow fast it moves, in pixels per frame
path_end_dowhat will happen at the end

You can change path_speed at any time to speed the instance up or slow it down. You can also change path_end_do. The new value applies the next time the instance reaches an end. path_end_do is one of the four end actions, not a number, so path_end_do = 2; is refused.

What happens at the end

The third argument to path_start decides. There are four choices:

ConstantAt the end of the path
PATH_STOPthe instance stops at the end of the route, and its path becomes nothing
PATH_LOOPit starts over from the beginning
PATH_PING_PONGit turns around and follows the path backward, then forward again
PATH_CONTINUEit leaves the path and keeps moving in a straight line in the direction it was last heading
Four small diagrams: PATH_STOP, PATH_LOOP, PATH_PING_PONG and PATH_CONTINUEFour small diagrams: PATH_STOP, PATH_LOOP, PATH_PING_PONG and PATH_CONTINUE
Four small diagrams: PATHSTOP, PATHLOOP, PATHPINGPONG and PATH_CONTINUE

The picture shows an open path. PATH_LOOP does not travel back: the instance jumps from the last point straight to the first.

On a closed path, PATH_LOOP is seamless, because the last point and the first point are the same place. On an open path, PATH_LOOP snaps back from the last point to the first in one frame. That is by design. If you want the instance to travel back instead, use PATH_PING_PONG.

PATH_CONTINUE hands the instance over to ordinary movement. It gets a speed equal to its path_speed and a direction worked out from its last stretch of travel, so gravity and friction apply to it from then on.

The Path End event

An event is a block of code the engine runs when something happens. The Path End event runs when the instance reaches the end of its route:

The instance has already been moved for that frame when the event runs, so it is at the end of the route (or, after a loop, just past the start). That makes this the right place to turn a sprite around, start an animation, or give the instance its next path:

// Path End event of a guard: turn around at the end of each lap
flip_x = !flip_x;

For PATH_STOP, calling path_start inside the event begins a fresh route from that spot. When you assign a path to an instance from the Room Editor tab, the editor offers to add a Path End event for you.

Reading a path without following it

These functions describe a path asset. They work on any path, whether or not anything is following it.

FunctionReturns
path_point_count(p)how many points it has
path_point_x(p, i), path_point_y(p, i)the position of point i, counting from 0. An i that is out of range gives 0
path_length(p)the total length in pixels, measured after smoothing
path_x_at(p, pos), path_y_at(p, pos)the position at pos, from 0 to 1

The point functions see the points the engine actually walks. On a smoothed path that means the many short pieces, so path_point_count is larger than the number of points you clicked.

What a path cannot do

Everything in this category

Following: path_start, path_stop. Variables: path, path_pos, path_speed, path_end_do. Reading: the five functions in the table above. The four PATH_ constants and the Path End event are linked in the sections above.

See also: Motion and pathingThe Paths tabpath_startpath_endpath_end_doPATH_LOOP